package cc.avatar.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
 * 一次货运
 */
@Entity
@Table(name="t_carriage") 
public class Carriage extends AccountingEvent{
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long id;
	
	//是否为退板，这种运货单是不计运费的.
	@Column(columnDefinition="Bit default 0")
	private boolean returned = false;
	
	@Column(columnDefinition="Bit default 0")
	private boolean carriageForward = true; //运费未付
	
	@ManyToOne(cascade=CascadeType.ALL)
	private Party carrier;
	
	private Date whenOccurred;
	
	@ManyToOne(cascade=CascadeType.ALL)
	@JoinColumn(name = "sale_id")
    private Sale sale;
	
	@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
	@JoinColumn(name="carriage_id")
	private Set<CarriageItem> items = new HashSet<CarriageItem>();
	
	public Set<CarriageItem> getItems() {
		return items;
	}

	public void setItems(Set<CarriageItem> items) {
		this.items = items;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Party getCarrier() {
		return carrier;
	}

	public boolean isCarriageForward() {
		return carriageForward;
	}

	public void setCarriageForward(boolean carriageForward) {
		this.carriageForward = carriageForward;
	}

	public void setCarrier(Party carrier) {
		this.carrier = carrier;
	}

	private int getTotalAmount(){
		int total = 0;
		for (CarriageItem item: this.getItems()){
			int amount = item.getAmount();
			total += amount;
		}
		return total;
	}
	
	private int getTotalCarriageAmount(){
		int total = 0;
		for (CarriageItem item: this.getItems()){
			int amount = item.getAmount();
			if (amount >=0 || (amount < 0 && !this.isReturned()))
			    total += amount;
		}
		return total;		
	}
	
	public Sale getSale() {
		return sale;
	}

	public void setSale(Sale sale) {
		this.sale = sale;
	}

	//总运费 - 当数量为负时代表换板或退板，不用计算运费
	public double getTotalFreight(){
		return this.sale.getCustomer().getFreightPrice()*getTotalCarriageAmount();
	}
	
	//某个产品的总数量
	protected int getProductQuantities(Product p){
		int total = 0;
		for (CarriageItem item: this.getItems()){
			if (item.getProduct().getId().equals(p.getId()))
			    total += item.getAmount();
		}
		return total;
	}
	
	public void addItem(String customer, CarriageItem item){
		StringBuilder s = new StringBuilder();
		s.append(Sale.TIMEFORMAT.format(this.getWhenOccurred()));
		s.append(" ").append(this.carrier.getName());
		s.append(" (").append(customer).append(")");
		if (this.isReturned())
			s.append(" 退板");
        item.setSummary(s.toString());
		this.items.add(item);
	}
	
	public boolean isReturned() {
		return returned;
	}

	public void setReturned(boolean returned) {
		this.returned = returned;
	}

	@Override
	public Party getStakeholder() {
		return this.carrier;
	}

	@Override
	public Date getWhenOccurred() {
		return this.whenOccurred;
	}
	
	@Override
	protected PostingRule getDefaultRule(){
		return PostingRule.freightPostingRule;
	}
	
	public void setWhenOccurred(Date time){
		this.whenOccurred = time;
	}
	
	@Override
	public String toString() {
		return "销售单-" + this.getSale().getTrackingNumber()
		     + " 客户-" + this.getSale().getCustomer().getName()
		     + " 总块数-" + getTotalAmount();
	}

	@Override
	public String getAtt1() {
		return this.sale.getTrackingNumber();
	}

	@Override
	public String getAtt2() {		
		return this.getSale().getCustomer().getName();

	}

	@Override
	public String getAtt3() {
		return String.valueOf(this.getTotalAmount());
	}
	
}
